package cz.muni.fi.pa165.sportclubmanager.web;

import cz.muni.fi.pa165.sportclubmanager.dto.AgeCategoryDTO;
import cz.muni.fi.pa165.sportclubmanager.dto.TeamDTO;
import cz.muni.fi.pa165.sportclubmanager.service.AgeCategoryService;
import cz.muni.fi.pa165.sportclubmanager.service.TeamService;
import java.util.List;
import net.sourceforge.stripes.action.*;
import net.sourceforge.stripes.controller.LifecycleStage;
import net.sourceforge.stripes.integration.spring.SpringBean;

@UrlBinding("/t/{$event}/{team.id}")
public class TeamsActionBean implements ActionBean {

    private static final String INDEX = "/teams/index.jsp";
    private static final String EDIT = "/teams/edit.jsp";
    private ActionBeanContext context;
    @SpringBean
    protected TeamService teamService;
    @SpringBean
    protected AgeCategoryService ageCategoryService;
    private TeamDTO team;
    private AgeCategoryDTO ageCategory;

    /**
     * AgeCategory list for select box with category selection
     *
     * @return All possible Age categories
     */
    public List<AgeCategoryDTO> getAllAgeCategories() {
        return ageCategoryService.findAll();
    }

    @Before(stages = LifecycleStage.BindingAndValidation, on = {"save", "edit"})
    public void beforeSaveAndEdit() {
        String ids = context.getRequest().getParameter("team.id");
        if (ids == null) {
            return;
        }
        team = teamService.find(Long.parseLong(ids));
        ageCategory = team.getAgeCategory();

        String aids = context.getRequest().getParameter("ageCategory.id");
        if (aids != null) {
            ageCategory = ageCategoryService.find(Long.parseLong(aids));
            team.setAgeCategory(ageCategory);
        }
    }

    @DefaultHandler
    public Resolution index() {
        return new ForwardResolution(INDEX);
    }

    public Resolution save() {
        try {
            teamService.update(team);
            getContext().getMessages().add(new SimpleMessage("team updated"));
        } catch (Exception ex) {
            getContext().getMessages().add(new SimpleMessage("error: " + ex.getLocalizedMessage()));
        }        
        return new RedirectResolution(this.getClass(), "index");
    }

    public Resolution edit() {
        return new ForwardResolution(EDIT);
    }

    public Resolution add() {
        try {
            teamService.create(team);
            getContext().getMessages().add(new SimpleMessage("team created"));
        } catch (Exception ex) {
            getContext().getMessages().add(new SimpleMessage("error: " + ex.getLocalizedMessage()));
        }                
        return new RedirectResolution(this.getClass(), "index");
    }

    public Resolution delete() {
        try {
            teamService.remove(team);
            getContext().getMessages().add(new SimpleMessage("team removed"));
        } catch (Exception ex) {
            getContext().getMessages().add(new SimpleMessage("error: " + ex.getLocalizedMessage()));
        }                
        return new RedirectResolution(this.getClass(), "index");
    }

    public List<TeamDTO> getTeams() {
        return teamService.findAll();
    }

    @Override
    public void setContext(ActionBeanContext context) {
        this.context = context;
    }

    @Override
    public ActionBeanContext getContext() {
        return context;
    }

    public TeamDTO getTeam() {
        return team;
    }

    public void setTeam(TeamDTO team) {
        this.team = team;
    }

    public AgeCategoryDTO getAgeCategory() {
        return ageCategory;
    }

    public void setAgeCategory(AgeCategoryDTO ageCategory) {
        this.ageCategory = ageCategory;
    }
}